package cc.sjfx.sharedoc.activity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.activity.user.UserLoginActivity;
import cc.sjfx.sharedoc.cache.UserCache;
import cc.sjfx.sharedoc.function.Preference;
import cc.sjfx.sharedoc.utils.CacheUtil;
import cc.sjfx.sharedoc.widget.anotation.ViewInject;
import cc.sjfx.sharedoc.widget.view.MyHeadLayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class BasicFragmentActivity extends FragmentActivity {
	protected BasicFragmentActivity context;
	protected Preference preference;
	protected MyHeadLayout headLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		CacheUtil.activitys.add(context);
		preference = Preference.instance(context);
		DisplayMetrics dm = new DisplayMetrics();
		headLayout = (MyHeadLayout) findViewById(R.id.headLayout);
		getWindowManager().getDefaultDisplay().getMetrics(dm);
	}

	public void setTitle(String title) {
		headLayout = (MyHeadLayout) findViewById(R.id.headLayout);
		headLayout.setTitle(title);
	}

	public void setTitleTextVisibility(int visibility) {
		headLayout = (MyHeadLayout) findViewById(R.id.headLayout);
		headLayout.setTitleTextVisibility(visibility);
	}

	public void setButtonVisibility(int visibility) {
		headLayout = (MyHeadLayout) findViewById(R.id.headLayout);
		headLayout.setButtonVisibility(visibility);
	}

	public void setSetButtonVisibility(int visibility) {
		headLayout = (MyHeadLayout) findViewById(R.id.headLayout);
		headLayout.setSetButtonVisibility(visibility);
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		initView();
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		initView();
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
		initView();
	}

	/**
	 * 初始化findviewbyid注解
	 * 
	 * @return
	 */
	private final void initView() {
		Field[] fields = getClass().getDeclaredFields();
		if (fields != null && fields.length > 0) {
			for (Field field : fields) {
				try {
					field.setAccessible(true);
					if (field.get(this) != null)
						continue;
					ViewInject viewInject = field
							.getAnnotation(ViewInject.class);
					if (viewInject != null) {
						int viewId = viewInject.id();
						field.set(this, findViewById(viewId));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void setExitActivityMap(String key, Activity activity) {
		List<Activity> list = CacheUtil.exitMap.get(key);
		if (list == null) {
			list = new ArrayList<Activity>();
		}
		list.add(activity);
		CacheUtil.exitMap.put(key, list);
	}

	protected void exitMap(String key) {
		if (CacheUtil.exitMap.get(key) != null) {
			for (Activity activity : CacheUtil.exitMap.get(key)) {
				activity.finish();
			}
		}
	}

	protected void exitAll() {
		for (Activity activity : CacheUtil.activitys) {
			activity.finish();
		}

	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
	}

	public void finishResult() {
		setResult(RESULT_OK);
		this.finish();
	}

	public void finishSimple() {
		super.finish();
	}

	public void startActivity(Bundle bundle, Class<?> target) {
		Intent intent = new Intent(this, target);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
	}

	public void startActivityNeedLogin(Bundle bundle, Class<?> target) {
		if (UserCache.isLogin) {
			startActivity(bundle, target);
		} else {
			startActivity(bundle, UserLoginActivity.class);
		}
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
	}

	public void startForResultNeedLogin(Bundle bundle, int requestCode,
			Class<?> target) {
		if (UserCache.isLogin) {
			Intent intent = new Intent(this, target);
			if (bundle != null) {
				intent.putExtras(bundle);
			}
			startActivityForResult(intent, requestCode);
		} else {
			startActivity(bundle, UserLoginActivity.class);
		}
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
	}

	public void startForResult(Bundle bundle, int requestCode, Class<?> target) {
		Intent intent = new Intent(this, target);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivityForResult(intent, requestCode);
		overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
	}

}
